package com.qiku.service.impl;

import com.qiku.dao.RoleMapper;
import com.qiku.domin.*;
import com.qiku.service.RoleService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> findAllRole(RoleVO roleVO) throws InvocationTargetException, IllegalAccessException {

        Role role = new Role();
        // 对象属性拷贝
        BeanUtils.copyProperties(role , roleVO);

        List<Role> allRole = roleMapper.findAllRole(role);
        return allRole;
    }



    @Override
    public void saveRole(Role role) {
        role.setCreatedTime(new Date());
        role.setUpdatedTime(new Date());
        role.setCreatedBy("admin");
        role.setUpdatedBy("admin");
        roleMapper.saveRole(role);
    }

    @Override
    public void UpdateRole(Role role) {
        role.setUpdatedTime(new Date());
        roleMapper.updateRole(role);
    }


    @Override
    public void deeteRole(Integer id) {
        roleMapper.deleteRoleContextMenu(id);
        roleMapper.deleteRole(id);
    }

    @Override
    public List<String> findMenuBuRoleId(Integer roleid) {
        List<String> roleId = roleMapper.findMenuBuRoleId(roleid);
        return roleId;
    }

    @Override
    public void RoleContextMenu(RoleMenuVo roleMenuVo) {

        // 先把角色之前与菜单的关系清楚掉
        roleMapper.deleteRoleContextMenu(roleMenuVo.getRoleId());

        for (Integer mid : roleMenuVo.getMenuIdList()){
            Role_menu_relation role_menu_relation = new Role_menu_relation();
            role_menu_relation.setMenuId(mid);
            role_menu_relation.setRoleId(roleMenuVo.getRoleId());
            role_menu_relation.setCreatedTime(new Date());
            role_menu_relation.setUpdatedTime(new Date());
            role_menu_relation.setCreatedBy("admin");
            role_menu_relation.setUpdatedby("admin");
            roleMapper.RoleContextMenu(role_menu_relation);

        }
    }

    @Override
    public List<Menu> fingAllMenu() {
        List<Menu> menuList = roleMapper.findAllMenu();
        return menuList;
    }


}
